package com;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

//@Component
@Aspect
public class LogAspect {

    //@Before("execution(* com.service.impl.UserServieImpl.delete())")
    public void before() {
        System.out.println("before****");
    }

    //after通知是不管连接点方法正常执行还是抛了异常，它都会执行，
    // 所以after通知也称之为最终通知
    //@After("execution(* com.service.impl.UserServieImpl.delete())")
    // @After("execution(* com.service.impl.UserServieImpl.add(int,int))")
    public void after() {
        System.out.println("after****");
    }

    //@Around("execution(* com.service.impl.UserServieImpl.add(int,int))")
    // @Around("execution(* com.service.impl.UserServieImpl.delete())")
    public Object around(ProceedingJoinPoint joinPoint) {
        //相当于Before通知
        System.out.println("around before----");
//获取方法的参数值
        //joinPoint.getArgs()//
        //获取目标对象,
        //joinPoint.getTarget()
        //得到方法的签名信息
//        joinPoint.getSignature()

        Object result = null;
        try {
            //下面的代码是让执行链继续执行，简介导致连接点方法以及下一个通知会得到执行
            result = joinPoint.proceed();
            //相当于AfterReturning通知
            System.out.println("result:" + result);
        } catch (Throwable e) {
            //类似异常通知，环绕通知是可以捕获并处理异常，
            // 但是异常通知只是接收到异常，并不能捕获处理
            e.printStackTrace();
        }
        //相当于after通知
        System.out.println("around after----");

        return result;
    }


    //@AfterReturning(value = "execution(* com.service.impl.UserServieImpl.add(int,int))",returning = "xx")
    public void afterReturning(int xx) {
        //方法参数名与@AfterReturning注解中的returing属性指定的值一样

        //如果你不想接收方法的返回值，你可以不用添加方法参数

        System.out.println("asdfasdf");
        // System.out.println("xx就是用来接收方法返回（正常执行）之后的结果:" + xx);
    }


    @AfterThrowing(value = "execution(* com.service.impl.UserServieImpl.add(int,int))", throwing = "ex")
    public void afterThrowing(RuntimeException ex) {
        //System.out.println("ex-----advice" );
        System.out.println("ex-----advice  --" + ex.getMessage() + "-----");

    }
}
